package indi.kider.server.woniubus.service;

import indi.kider.common.constant.DevLockStateCons;
import indi.kider.common.constant.EmployeeRoleCons;
import indi.kider.common.model.apabus.Rent;
import indi.kider.common.model.apartment.*;
import indi.kider.common.model.employee.Employee;
import indi.kider.server.apabus.mapper.RentMapper;
import indi.kider.server.apartment.mapper.*;
import indi.kider.server.apartment.model.DevLockOpenDoorLogVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.vankid.common.model.base.CoreException;
import pers.vankid.common.utils.page.PageRequest;
import pers.vankid.common.utils.page.PageResponse;
import pers.vankid.common.utils.page.PageUtil;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@SuppressWarnings("ALL")
public class DevLockServiceImpl implements DevLockService {

    @Autowired
    private DevLockOpenDoorLogMapper devLockOpenDoorLogMapper;

    @Autowired
    private DevLockCardMapper        devLockCardMapper;
    @Autowired
    private DevLockFingerprintMapper devLockFingerprintMapper;
    @Autowired
    private DevLockPasswordMapper    devLockPasswordMapper;
    @Autowired
    private RentMapper               rentMapper;
    @Override
    public PageResponse<List<Map>> getOpenDoorLogList(PageRequest<DevLockOpenDoorLogVo> request, Employee employee) {
        DevLockOpenDoorLogVo vo = request.getQuery();
        if(!employee.getRoles().equals(EmployeeRoleCons.ADMIN)) {
            vo.setEnterpriseId(employee.getEnterpriseId());
        }
        PageUtil.start(request);
        return PageUtil.getPageResponse(devLockOpenDoorLogMapper.getOpenDoorLogList(vo));
    }


    @Override
    public PageResponse<List<Map>> getOpenDoorSumLogList(PageRequest<DevLockOpenDoorLogVo> request, Employee employee) {
        DevLockOpenDoorLogVo vo = request.getQuery();
        if(!employee.getRoles().equals(EmployeeRoleCons.ADMIN)) {
            vo.setEnterpriseId(employee.getEnterpriseId());
        }
        PageUtil.start(request);
        return PageUtil.getPageResponse(devLockOpenDoorLogMapper.getOpenDoorSumLogList(vo));
    }

    @Autowired
    DevLockMapper devLockMapper;

    @Autowired
    HousesMapper housesMapper;

    private static final String SPLITTER = "_";

    @Override
    public DevLock addDevLock(DevLock devLock) throws CoreException {
        DevLock selLock = new DevLock();
        selLock.setHouseId(devLock.getHouseId());
        selLock.setState(DevLockStateCons.INSTALL);
        int cnt = devLockMapper.selectCount(selLock);
        if (cnt > 0) {
            throw new CoreException("该房间已存在锁具,请先解绑");
        }
        selLock.setHouseId(devLock.getHouseId());
        selLock.setState(DevLockStateCons.NOINSTALL);
        cnt = devLockMapper.selectCount(selLock);
        if (cnt > 0) {
            throw new CoreException("该房间已存在锁具,请先解绑");
        }

        DevLock selLock2 = new DevLock();
        selLock2.setImei(devLock.getImei());
        cnt = devLockMapper.selectCount(selLock2);
        if (cnt > 0) {
            throw new CoreException("该imei已被使用,不能重复添加");
        }
        devLock.setCreateDate(new Date());
        devLockMapper.insertSelective(devLock);
        return devLock;
    }

    @Override
    public DevLock lockByID(Long id) throws CoreException {
        return devLockMapper.selectByPrimaryKey(id);
    }

    @Override
    public DevLock lockByHouseId(Long houseId) throws CoreException {
        DevLock devLock = new DevLock();
        devLock.setHouseId(houseId);
        return devLockMapper.selectOne(devLock);
    }

    public int updateDevLock(DevLock devLock) throws CoreException {
        DevLock dl = devLockMapper.selectByPrimaryKey(devLock.getId());
        if(dl.getImei().equals(devLock.getImei())){
            return devLockMapper.updateByPrimaryKeySelective(devLock);
        }
        DevLock selLock2 = new DevLock();
        selLock2.setImei(devLock.getImei());
        int cnt = devLockMapper.selectCount(selLock2);
        if (cnt > 0) {
            throw new CoreException("该imei已被其他使用使用,不能重复添加");
        }
        return devLockMapper.updateByPrimaryKeySelective(devLock);
    }

    /**
     * 根据房源查询表具列表
     *
     * @param rentLock
     * @return
     */
    @Override

    public List<RentLock> getRentLock(RentLock rentLock) {
        return devLockMapper.getRentLock(rentLock);
    }

    /**
     * 解除绑定锁具
     *
     * @param lockId
     * @param housesId
     */
    @Override
    public void cleanHouseLock(Long lockId, Long housesId) throws Exception {
        log.info("解除绑定锁具lockId:{},housesId:{}",lockId,housesId);
        devLockMapper.deleteByPrimaryKey(lockId);
        //删除卡片
        Example cardExample = new Example(DevLockCard.class);
        cardExample.createCriteria().andEqualTo("lockId",lockId+"");
        devLockCardMapper.deleteByExample(cardExample);

        //删指纹
        Example rentExample = new Example(Rent.class);
        rentExample.createCriteria().andEqualTo("housesId",housesId);
        List<Rent> list = rentMapper.selectByExample(rentExample);
        ArrayList<Long> arr = new ArrayList<>();
        for(Rent rent : list){
            arr.add(rent.getTenantId());
        }
        if(arr.size()>0){
            Example fpExample = new Example(DevLockFingerPrint.class);
            fpExample.createCriteria().andIn("tenantId",arr);
            devLockFingerprintMapper.deleteByExample(fpExample);
        }
        //删密码
        Example pwdExample = new Example(DevLockPassword.class);
        pwdExample.createCriteria().andEqualTo("lockId",lockId+"");
        devLockPasswordMapper.deleteByExample(pwdExample);
    }
}
